package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.QuestionItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Question;
import com.itheima.service.QuestionItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author TJH
 * @date 2019-12-15 9:19
 */

@Service(interfaceClass = QuestionItemService.class)
public class QuestionItemServiceImpl implements QuestionItemService {

    @Autowired
    private QuestionItemDao questionItemDao;

    /**
     * 添加问题
     * @param question
     */
    @Override
    public void add(Question question) {
        questionItemDao.add(question);
    }

    /**
     * 修改问题
     * @param question
     */
    @Override
    public void edit(Question question) {
        questionItemDao.edit(question);
    }

    /**
     * 根据id查询问题
     * @param id
     * @return
     */
    @Override
    public Question findQuestionItemById(Integer id) {
        return questionItemDao.findQuestionItemById(id);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //查出所有信息
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //分页助手打前站
        PageHelper.startPage(currentPage,pageSize);
        if (queryString != null&&queryString.length()>0){
            queryString="%"+queryString+"%";
        }
        Page<Question> page = questionItemDao.findPage(queryString);
        long total = page.getTotal();
        List<Question> rows = page.getResult();

        return new PageResult(total,rows);
    }

    /**
     * 根据id删除问题
     * @param id
     */
    @Override
    public void delete(Integer id) {
        try{
            questionItemDao.deletes_que_ireByQueId(id);
            questionItemDao.deleteQuestionById(id);
        }catch (Exception e){
            throw new RuntimeException();
        }

    }
}
